package com.im.partchat.utils;

import android.content.Context;
import android.view.View;

import com.im.partchat.R;
import com.im.partchat.topic.bean.TopicBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ValueUtil {
	
	private static final int DAY = 24 * 60 * 60;// 天
	private static final int HOUR = 60 * 60;// 小时
	private static final int MINUTE = 60;// 分钟

	public static List<String> stringToArrayList(String str) {
		List<String> investorFoucusDirectionList = new ArrayList<>();
		if (str != null) {
			String[] zu = str.split("\\,");
			for (int i = 0; i < zu.length; i++) {
				if (zu[i].equals("")) {
					continue;
				}
				investorFoucusDirectionList.add(zu[i]);
			}
		}
		return investorFoucusDirectionList;
	}
	
	public static String arrayListToString(List<String> list) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i));
			if (i != list.size() - 1) {
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	public static long getTimeLong(String sTime) throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(sTime);

		return date.getTime();
	}

	/**
	 * 根据时间戳获取描述性时间，如3分钟前，1天前
	 * 
	 * @param timestamp
	 *            时间戳 单位为毫秒
	 * @return 时间字符串
	 */
	public static String getTimeStringFromNow(long timestamp) {
		long currentTime = System.currentTimeMillis();
		long timeGap = (currentTime - timestamp) / 1000;// 与现在时间相差秒数
		String timeStr = null;
		if (timeGap > DAY) {// 1天以上
			timeStr = getSimpleDate(new Date(timestamp));
		} else if (timeGap > HOUR) {// 1小时-24小时
			timeStr = timeGap / HOUR + "小时前";
		} else if (timeGap > MINUTE) {// 1分钟-59分钟
			timeStr = timeGap / MINUTE + "分钟前";
		} else {// 1秒钟-59秒钟
			timeStr = "刚刚";
		}
		return timeStr;
	}

	public static String getTimeStringFromEndTime(long endtime) {
		long currentTime = System.currentTimeMillis();
		long timeGap = (endtime - currentTime) / 1000;// 与现在时间相差秒数
		String timeStr = null;
		 if (timeGap > DAY) {// 1天以上
			timeStr = ""+timeGap / DAY ;
		}  else {// 1秒钟-59秒钟
			timeStr = "1";
		}
		return timeStr;
	}
	
	public static String getSimpleDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		return time;
	}

	
	/**
	 * 得到格式化后的系统时间 精确到秒
	 * 
	 * @param date
	 * @return
	 */
	public static String getSimpleTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		return time;
	}

	/**
	 * 得到格式化后的系统时间 精确到秒
	 * 
	 * @param datestr
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Date getSimpleData(String datestr)
			throws java.text.ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = sdf.parse(datestr);
		return time;
	}

	public static void resetLayoutParams(Context mContext, View iv,
			TopicBean bean) {
		int max_photo_width = (int) mContext.getResources().getDimension(R.dimen.max_photo_width);
		if (bean.getPhotoOriginalHeight() == 0) {
			bean.setPhotoOriginalHeight(max_photo_width);
		}
		if (bean.getPhotoOriginalWidth() == 0) {
			bean.setPhotoOriginalWidth(max_photo_width);
		}
		if (bean.getPhotoOriginalHeight() < bean.getPhotoOriginalWidth()) {
			// 图片很扁
			iv.getLayoutParams().width = bean.getPhotoOriginalWidth() > max_photo_width ? max_photo_width
					: bean.getPhotoOriginalWidth();
			iv.getLayoutParams().height = (bean.getPhotoOriginalHeight() * iv
					.getLayoutParams().width) / bean.getPhotoOriginalWidth();
		} else {
			// 图片很高
			iv.getLayoutParams().height = bean.getPhotoOriginalHeight() > max_photo_width ? max_photo_width
					: bean.getPhotoOriginalHeight();
			iv.getLayoutParams().width = (bean.getPhotoOriginalWidth() * iv
					.getLayoutParams().height) / bean.getPhotoOriginalHeight();
		}
	}

	
	
	public static int getIntFromStringWithCatch(String idString){
		int intvalue = 0;
		try {
			intvalue = Integer.parseInt(idString);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return intvalue;
	}
}
